package com.example.mall.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.mall.domain.BestSellingProducts;
import com.example.mall.dto.BestSellingProductsDto;
import com.example.mall.mapper.BestSellingProductsMapper;
import com.example.mall.query.BestSellingProductsQuery;
import com.example.mall.query.PageDTO;
import com.example.mall.service.IBestSellingProductsService;

import com.example.mall.util.AssertUtil;
import com.example.mall.util.CodeGenerateUtils;
import com.example.mall.util.CopyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 热销商品分析表 服务实现类
 * </p>
 *
 * @author whale
 * @since 2025-05-16
 */
@Service
public class BestSellingProductsServiceImpl extends ServiceImpl<BestSellingProductsMapper, BestSellingProducts> implements IBestSellingProductsService {

    @Autowired
    private BestSellingProductsMapper bestSellingProductsMapper;

    @Override
    public PageDTO<BestSellingProductsDto> queryPage(BestSellingProductsQuery bestSellingProductsQuery) {
        if (bestSellingProductsQuery == null) {
            bestSellingProductsQuery = new BestSellingProductsQuery();
        }
        Page<BestSellingProducts> page = bestSellingProductsQuery.toMpPageByDefaultCreateTimeAsc();
        Page<BestSellingProducts> cartPage = lambdaQuery()
                .like(bestSellingProductsQuery.getProductId()!= null, BestSellingProducts::getProductId, bestSellingProductsQuery.getProductId())
                .like(bestSellingProductsQuery.getSellingProductsId()!= null, BestSellingProducts::getSellingProductsId, bestSellingProductsQuery.getSellingProductsId())
                .like(bestSellingProductsQuery.getOrderCount()!= null, BestSellingProducts::getOrderCount, bestSellingProductsQuery.getOrderCount())
                .like(bestSellingProductsQuery.getSalesAmount()!= null, BestSellingProducts::getSalesAmount, bestSellingProductsQuery.getSalesAmount())
                .apply(bestSellingProductsQuery.getStatDate() != null,
                        "DATE_FORMAT(stat_date, '%Y-%m-%d') = DATE_FORMAT({0}, '%Y-%m-%d')",
                        bestSellingProductsQuery.getStatDate())
                .orderByDesc(BestSellingProducts::getUpdateDate)
                .page(page);
        return PageDTO.of(cartPage, BestSellingProductsDto.class);
    }

    @Override
    public BestSellingProducts queryBestSellingProductsById(Long id) {
        AssertUtil.isNotNull(id,"商品分析唯一标识不能为空");
        return bestSellingProductsMapper.selectById(id);
    }

    @Override
    public List<BestSellingProducts> queryBestSellingProductsList() {
        return this.list();
    }

    @Override
    public void insert(BestSellingProductsDto bestSellingProductsDto) {
        BestSellingProducts bestSellingProducts = new BestSellingProducts();
        CopyUtils.copyProperties(bestSellingProductsDto,bestSellingProducts);
        bestSellingProducts.setSellingProductsId(Math.toIntExact(CodeGenerateUtils.generateUniqueId()));
        bestSellingProducts.setCreateDate(new Date());
        bestSellingProducts.setUpdateDate(new Date());
        bestSellingProductsMapper.insert(bestSellingProducts);
    }

    @Override
    public void update(BestSellingProductsDto bestSellingProductsDto) {
        AssertUtil.isNotNull(bestSellingProductsDto.getSellingProductsId(),"sellingProductsId不能为空");
        BestSellingProducts bestSellingProducts = bestSellingProductsMapper.selectById(bestSellingProductsDto.getSellingProductsId());
        AssertUtil.isNotNull(bestSellingProducts,"热销商品分析数据不能为空");
        CopyUtils.copyProperties(bestSellingProductsDto,bestSellingProducts);
        bestSellingProducts.setUpdateDate(new Date());
        bestSellingProductsMapper.updateById(bestSellingProducts);
    }

    @Override
    public void delete(Long id) {
        AssertUtil.isNotNull(id,"唯一标识不能为空");
        BestSellingProducts bestSellingProducts = bestSellingProductsMapper.selectById(id);
        AssertUtil.isNotNull(bestSellingProducts,"热销商品分析数据不能为空");
        bestSellingProductsMapper.deleteById(bestSellingProducts);
    }

    @Override
    public void deleteBatch(List<Long> ids) {
        AssertUtil.isNotNull(ids,"唯一标识不能为空");
        List<BestSellingProducts> bestSellingProducts = bestSellingProductsMapper.selectBatchIds(ids);
        AssertUtil.isNotNull(bestSellingProducts,"热销商品分析数据不能为空");
        bestSellingProductsMapper.deleteBatchIds(bestSellingProducts);
    }
}
